import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:toys_flutter/app_routes.dart';
import 'package:toys_flutter/pages/tabbar/home.dart';
import 'package:toys_flutter/pages/tabbar/lives.dart';
import 'package:toys_flutter/pages/tabbar/messages.dart';
import 'package:toys_flutter/pages/tabbar/news.dart';
import 'package:toys_flutter/pages/tabbar/user.dart';
import 'package:toys_flutter/store/app.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => AppState()),
      ],
      child: const MyApp(),
    ),
  );
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    _initializeAppState();
  }

  // 初始化 AppState
  void _initializeAppState() async {
    await Provider.of<AppState>(context, listen: false).loadFromPrefs();
  }

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Toys',
      theme: ThemeData(
        scaffoldBackgroundColor: Colors.white, // 设置全局背景颜色为白色
        primaryColor: Colors.blue,
        appBarTheme: const AppBarTheme(
          backgroundColor: Colors.white, // 设置全局 AppBar 背景颜色
          foregroundColor: Colors.black, // 设置 AppBar 上文本和图标的颜色
        ),
        textButtonTheme: TextButtonThemeData(
          style: TextButton.styleFrom(
            foregroundColor: Colors.white,
            backgroundColor: Colors.blue, // 按钮背景颜色
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(8), // 圆角
            ),
          ),
        ),
      ),
      initialRoute: '/', // 初始路由
      routes: AppRoutes.getRoutes(), // 使用路由文件中的路由
      home: Scaffold(
        body: IndexedStack(
          index: _currentIndex,
          children: const [
            HomePage(),
            LivesPage(),
            MessagesPage(),
            NewsPage(),
            UserPage(),
          ],
        ),
        bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed, // 设置为 fixed 类型
          currentIndex: _currentIndex,
          onTap: (index) {
            setState(() {
              _currentIndex = index;
            });
          },
          items: const [
            BottomNavigationBarItem(
              icon: Icon(Icons.home_outlined),
              activeIcon: Icon(Icons.home, color: Colors.blue),
              label: '首页',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.live_tv_outlined),
              activeIcon: Icon(Icons.live_tv, color: Colors.blue),
              label: '直播圈',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.message_outlined),
              activeIcon: Icon(Icons.message, color: Colors.blue),
              label: '消息',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.newspaper_outlined),
              activeIcon: Icon(Icons.newspaper, color: Colors.blue),
              label: '资讯',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.person_outlined),
              activeIcon: Icon(Icons.person, color: Colors.blue),
              label: '我的',
            ),
          ],
        ),
      ),
    );
  }

  // Future<double> getDisplayRefreshRate() async {
  //   final double fps;
  //   if (defaultTargetPlatform == TargetPlatform.android) {
  //     fps = await MethodChannel('display').invokeMethod('getRefreshRate');
  //   } else {
  //     fps = 60.0; // iOS doesn't expose refresh rate directly
  //   }
  //   return fps;
  // }
}
